package com.lzxuni.modules.cropinformation.controller;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.annotation.SysLog;
import com.lzxuni.common.constant.LogConstant;
import com.lzxuni.common.utils.FileUtil;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.common.utils.date.DateUtil;
import com.lzxuni.modules.basics.entity.BasicTree;
import com.lzxuni.modules.basics.service.BasicsLandService;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.cropinformation.entity.TblPlantingInformation;
import com.lzxuni.modules.cropinformation.service.TblCropInformationService;
import com.lzxuni.modules.cropinformation.service.TblPlantingInformationService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 作物种植信息 前端控制器
 * </p>
 *
 * @author rdf
 * @since 2020-11-26
 */
@RestController
@RequestMapping("/LR_TblPlantingInformation/TblPlantingInformation")
public class TblPlantingInformationController extends BaseController{

    @Autowired
    private TblPlantingInformationService tblPlantingInformationService;
    @Autowired
    private TblCropInformationService tblCropInformationService;
    @Autowired
    private BasicsLandService basicsLandService;

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView("/LR_TblCropInformation/plantingInformation/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,String queryJson,String plantingYear,String cropModel){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        TblPlantingInformation tblPlantingInformation = JSON.parseObject(queryJson, TblPlantingInformation.class);
//        TblPlantingInformation tblPlantingInformation = new TblPlantingInformation();
//        if(StringUtils.isNotEmpty(cropName)){
//            cropInformation.setCropName(cropName);
//        }
//        if(StringUtils.isNotEmpty(cropModel)){
//            cropInformation.setCropModel(cropModel);
//        }
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
        Date asd = new Date();
        String shijian = sdf1.format(asd);
        if(StringUtils.isNotEmpty(tblPlantingInformation.getPlantingYear())){
            tblPlantingInformation.setPlantingYear(tblPlantingInformation.getPlantingYear());
        }else {
            tblPlantingInformation.setPlantingYear(shijian);
        }
        String sjId = tblPlantingInformation.getParentId();
//        String landIds =basicsLandService.queryLandIds(sjId);
        tblPlantingInformation.setJiedianId(sjId);
        PageData pageData = getPageData(tblPlantingInformationService.queryPage(pageParameter, tblPlantingInformation));
        return R.ok().put("data", pageData);
    }

    //表单页
    @RequestMapping("/Form")
    public ModelAndView Form(String keyValue, String queryJson) throws Exception {
        ModelAndView mv = new ModelAndView("/LR_TblCropInformation/plantingInformation/form");
        BasicTree basicTree = JSON.parseObject(queryJson, BasicTree.class);
        mv.addObject("basicTree", basicTree);
        return mv;
    }

    //保存
    @SysLog(categoryId = LogConstant.OPERATEID, module = "作物种植信息", operateType = LogConstant.INSERT)
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue,TblPlantingInformation tblPlantingInformation) {
        if(StringUtils.isNotEmpty(keyValue)){
            tblPlantingInformation.setPlantingId(keyValue);
            tblPlantingInformation.setLandId(tblPlantingInformation.getLandId());
//            tblPlantingInformation.setCropId(tblPlantingInformation.getCropId());
            tblPlantingInformation.setCropModela(tblPlantingInformation.getCropModela());
            tblPlantingInformation.setPlantingYear(tblPlantingInformation.getPlantingYear());
            tblPlantingInformationService.updateById(tblPlantingInformation);
//            return R.ok().put("data", "修改成功！");
            return R.ok("修改成功");
        }else {
            String plId = UuidUtil.get32UUID();
            tblPlantingInformation.setPlantingId(plId);
            tblPlantingInformation.setLandId(tblPlantingInformation.getLandId());
//            tblPlantingInformation.setCropId(tblPlantingInformation.getCropId());
            tblPlantingInformation.setCropModela(tblPlantingInformation.getCropModela());
            tblPlantingInformation.setPlantingYear(tblPlantingInformation.getPlantingYear());
            tblPlantingInformation.setCreateTime(new Date());
            tblPlantingInformationService.save(tblPlantingInformation);
//            return R.ok().put("data", "保存成功！");
            return R.ok("保存成功");
        }
    }
    //删除
    @SysLog(categoryId = LogConstant.OPERATEID,module = "作物种植信息",operateType = LogConstant.DELETE)
    @RequestMapping("/DeleteForm")
    public Object delete(String keyValue) {
        tblPlantingInformationService.removeById(keyValue);
        return R.ok("删除成功");
    }
    //导出
    @SysLog(categoryId = LogConstant.VIEWID,module = "作物种植信息导出",operateType = LogConstant.EXPORT)
    @RequestMapping("/Export")
    public void  Download(HttpServletResponse response, String queryJson, HttpServletRequest request) throws Exception {
        TblPlantingInformation tblPlantingInformation = JSON.parseObject(queryJson, TblPlantingInformation.class);
        BufferedOutputStream fos = new BufferedOutputStream(response.getOutputStream());
        String filename = "作物种植信息-" + DateUtil.DateToString(new Date(), "yyyyMMddHHmmss") + ".xls";
        String agent = request.getHeader("User-Agent");
        filename = FileUtil.encodeDownloadFilename(filename, agent);
        String contentType = request.getSession().getServletContext().getMimeType(filename);
        response.setContentType(contentType);
        response.setHeader("content-disposition", "attchment;filename=" + filename);
//        TblLoan tblLoan = setObject(queryJson, loanYear,categoryId);
        //获取当年年份
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
        Date asd = new Date();
        String shijian = sdf1.format(asd);
        if(StringUtils.isNotEmpty(tblPlantingInformation.getPlantingYear())){
            tblPlantingInformation.setPlantingYear(tblPlantingInformation.getPlantingYear());
        }else{
            tblPlantingInformation.setPlantingYear(shijian);
        }
        String zcId = tblPlantingInformation.getTreeId();
        tblPlantingInformation.setJiedianId(zcId);
        Workbook wb = null;
        try {
            wb = tblPlantingInformationService.exportTemplate(tblPlantingInformation);
            wb.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fos.close();
        }
    }
    //作物名称
    @RequestMapping("/GetTreeCrop")
    public Object getTree() {
        List<Tree> comptList = tblCropInformationService.getTree();
        return R.ok().put("data", comptList);
    }
    //作物型号(与作物名称联动)
    @RequestMapping("/GetTreekh")
    public Object getTreekh(String cropName,String cropId) {
            List<Tree> customList = tblCropInformationService.getTreekh(cropName);
            return R.ok().put("data", customList);
    }

}

